package com.hq.common.domain;

import com.hq.admin.domain.Operator;
import com.hq.admin.domain.OperatorContext;

/**
 * Created by He Zhijie on 2015/8/13.
 */
public class ContextHolder {

    private static final ThreadLocal<UserContext> USER_CONTEXT_THREAD_LOCAL = new ThreadLocal<UserContext>() {
        @Override
        protected UserContext initialValue() {
            return new UserContext();
        }
    };
    private static final ThreadLocal<OperatorContext> OPERATOR_CONTEXT_THREAD_LOCAL = new ThreadLocal<OperatorContext>() {
        @Override
        protected OperatorContext initialValue() {
            return new OperatorContext();
        }
    };

    private ContextHolder() {}

    public static UserContext getUserContext() {
        return USER_CONTEXT_THREAD_LOCAL.get();
    }

    public static void removeUserContext(){
        USER_CONTEXT_THREAD_LOCAL.remove();
    }

    public static long getUserId(){
        return USER_CONTEXT_THREAD_LOCAL.get().getUser().getId();
    }

    public static OperatorContext getOperatorContext(){
        return OPERATOR_CONTEXT_THREAD_LOCAL.get();
    }

    public static void removeOperatorContext(){
        OPERATOR_CONTEXT_THREAD_LOCAL.remove();
    }

    public static Operator getOperator(){
        return getOperatorContext().getOperator();
    }

    public static long getOperatorId(){
        return getOperator().getId();
    }
}
